﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

using AKMII.DMRA.Common;

namespace AKMII.DMRA.DataAccess
{
    public class SettingAccessor
    {
        public CommonSetting GetCommonSetting()
        {
            CommonSetting setting = null;
            const string SpName = "sp_Get_CommonSetting";
            SqlDataReader reader = null;
            try
            {
                reader = SqlHelper.ExecuteReader(Constants.ConnectionString, SpName, new object[] { });

                if (reader.Read())
                {
                    setting = new CommonSetting();
                    setting.SmtpServer = reader.GetString(0);
                    setting.SystemMailAddress = reader.GetString(1);
                    setting.MailAddressForSummaryReport = reader.GetString(2);
                    setting.MailPassword = reader.GetString(3);
                    setting.DownloadFtp = reader.GetString(4);
                    setting.UploadFtp = reader.GetString(5);
                    setting.FtpUserName = reader.GetString(6);
                    setting.FtpPassword = reader.GetString(7);
                    setting.PrintingHouseFtp = reader.GetString(8);
                    setting.PrintingHouseUserName = reader.GetString(9);
                    setting.PrintingHousePassword = reader.GetString(10);
                    setting.PrintingHouseFileName = reader.GetString(11);
                    setting.PrivateKeyPassword = reader.GetString(12);
                    setting.DMRARecipient = reader.GetString(13);
                    setting.PrintingHouseRecipient = reader.GetString(14);
                    setting.FolderForDownload = reader.GetString(15);
                    setting.FolderForDecrypt = reader.GetString(16);
                    setting.FolderForDecompress = reader.GetString(17);
                    setting.FolderForGenerateDmraFile = reader.GetString(18);
                    setting.FolderForGeneratePrintingHouseFile = reader.GetString(19);
                    setting.FolderForCompressDmraFile = reader.GetString(20);
                    setting.FolderForCompressPrintingHouseFile = reader.GetString(21);
                    setting.FolderForEncryptDmraFile = reader.GetString(22);
                    setting.FolderForEncryptPrintingHouseFile = reader.GetString(23);
                    setting.MDForAssignBlankZip = reader.GetString(24);
                    setting.CurrentYear = reader.GetString(25);
                    setting.FolderForSummaryReport = reader.GetString(26);
                    setting.LeadPerAgent = reader.GetInt32(27);
                    setting.LockMonth = reader.GetInt32(28);
                    setting.MaxStop = reader.GetInt32(29);
                    setting.MinStop = reader.GetInt32(30);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get common setting", ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return setting;
        }

        public List<MonthSetting> GetMonthSettings()
        {
            List<MonthSetting> settings = new List<MonthSetting>();
            const string SpName = "sp_Get_MonthSettings";
            SqlDataReader reader = null;

            try
            {
                reader = SqlHelper.ExecuteReader(Constants.ConnectionString, SpName, new object[] { });

                while (reader.Read())
                {
                    MonthSetting setting = new MonthSetting();

                    setting.MonthName = reader.GetInt32(0);
                    setting.StartDate = reader.GetDateTime(1);
                    setting.EndDate = reader.GetDateTime(2);
                    setting.CutOffStartDate = reader.GetDateTime(3);
                    setting.CutOffEndDate = reader.GetDateTime(4);
                    setting.UploadDate = reader.GetDateTime(5);

                    settings.Add(setting);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get month settings", ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return settings;
        }

        public bool UpdateMonthSetting(MonthSetting setting)
        {
            const string SpName = "sp_Update_MonthSetting";

            try
            {
                object[] parameters = new object[]
                {
                    Convert.ToInt32(setting.MonthName),
                    setting.StartDate,
                    setting.EndDate,
                    setting.CutOffStartDate,
                    setting.CutOffEndDate,
                    setting.UploadDate
                };

                SqlHelper.ExecuteReader(Constants.ConnectionString, SpName, parameters);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update month setting", ex);
                return false;
            }
            return true;
        }

        public bool UpdateCommonSetting(CommonSetting setting)
        {
            const string SpName = "sp_Update_CommonSetting";

            try
            {
                object[] parameters = new object[]
                {
                    setting.SmtpServer,
                    setting.SystemMailAddress,
                    setting.MailAddressForSummaryReport,
                    setting.MailPassword,
                    setting.DownloadFtp,
                    setting.UploadFtp,
                    setting.FtpUserName,
                    setting.FtpPassword,
                    setting.PrintingHouseFtp,
                    setting.PrintingHouseUserName,
                    setting.PrintingHousePassword,
                    setting.PrintingHouseFileName,
                    setting.PrivateKeyPassword,
                    setting.DMRARecipient,
                    setting.PrintingHouseRecipient,
                    setting.FolderForDownload,
                    setting.FolderForDecrypt,
                    setting.FolderForDecompress,
                    setting.FolderForGenerateDmraFile,
                    setting.FolderForGeneratePrintingHouseFile,
                    setting.FolderForCompressDmraFile,
                    setting.FolderForCompressPrintingHouseFile,
                    setting.FolderForEncryptDmraFile,
                    setting.FolderForEncryptPrintingHouseFile,
                    setting.MDForAssignBlankZip,
                    setting.CurrentYear,
                    setting.FolderForSummaryReport,
                    setting.LeadPerAgent,
                    setting.LockMonth,
                    setting.MaxStop,
                    setting.MinStop
                };

                SqlHelper.ExecuteNonQuery(Constants.ConnectionString, SpName, parameters);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update common setting", ex);
                return false;
            }

            return true;
        }

        public List<MonthSetting> GetMonthSettingByYear(string year)
        {
            List<MonthSetting> listMonth = new List<MonthSetting>();
            const string SpName = "sp_Get_MonthSettingByYear";
            SqlDataReader rd = null;
            try
            {
                object[] parameters = new object[]
                {
                    year
                };
                rd = SqlHelper.ExecuteReader(Constants.ConnectionString, SpName, parameters);
                while (rd.Read())
                {
                    MonthSetting currentMonth = new MonthSetting();
                    currentMonth.MonthName = int.Parse(rd[0].ToString());
                    currentMonth.StartDate = DateTime.Parse(rd[1].ToString());
                    currentMonth.EndDate = DateTime.Parse(rd[2].ToString());
                    listMonth.Add(currentMonth);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in Get MonthSetting By Year", ex);
            }
            finally
            {
                if(rd!=null)
                {
                    rd.Close();
                }
            }
            return listMonth;
        }

        public MonthSetting GetMonthSetting(int month)
        {
            MonthSetting setting = null;
            const string SpName = "sp_Get_MonthSetting";
            SqlDataReader reader = null;

            try
            {
                reader = SqlHelper.ExecuteReader(Constants.ConnectionString, SpName, new object[] { month });

                if (reader.Read())
                {
                    setting = new MonthSetting();
                    setting.MonthName = reader.GetInt32(0);
                    setting.StartDate = reader.GetDateTime(1);
                    setting.EndDate = reader.GetDateTime(2);
                    setting.CutOffStartDate = reader.GetDateTime(3);
                    setting.CutOffEndDate = reader.GetDateTime(4);
                    setting.UploadDate = reader.GetDateTime(5);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get month setting", ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return setting;
        }
    }
}
